package org.example.controller;

import cn.hutool.crypto.digest.BCrypt;
import com.google.gson.Gson;
import lombok.AllArgsConstructor;
import org.example.config.CaptureConfig;
import org.example.domain.dto.UserDTO;
import org.example.domain.po.User;
import org.example.service.IUserService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@AllArgsConstructor
@CrossOrigin(origins = {"*","null"})
@RequestMapping("/user")
public class UserController {
    private IUserService userService;
    private Gson gson = new Gson();

    @GetMapping("")
    public String index() {
        return "Hello World";
    }
    @PostMapping("/login")
    public int login(@RequestBody UserDTO userDTO, @RequestParam String key) {
        if(userDTO.getVerCode().toLowerCase().equals(CaptureConfig.CAPTURE_MAP.get(key))){
            User user=new User();
            user.setAccount(userDTO.getAccount());
            user.setPassword(userDTO.getPassword());
            return userService.queryUser(user)?1:0;
        }else{
            return 2;
        }

    }


    @PostMapping("/forgotPwd")
    public void forgotPwd(@RequestBody User user) {
         userService.forgotPwd(user);
    }

    @PostMapping("/register")
    public void register(@RequestBody User user) {
        user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt()));
        userService.save(user);
    }
    @GetMapping("/myInformation/{account}")
    public User myInformation(@PathVariable("account") String account){
        return userService.getOne(account);
    }

    @PutMapping("/update")
    public void update(@RequestBody User user){
        System.out.println(user+" sdadada");
        userService.updateById(user);
        System.out.println(user+" aaaaaaaaaaa");

    }


}
